import Vue from 'vue'
import App from './App.vue'
import $ from 'jquery'
import router from './router'
import store from './store/index'
import axios from 'axios'
import 'lib-flexible'
import Navigation from 'vue-navigation'
import VueLazyload from 'vue-lazyload'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import APPKEY from './assets/js/app/appKeywords'
import cookies from './assets/js/app/cookies'
// import Toast from './assets/js/common/toast'
import md5 from 'js-md5'
import layer from 'vue2-layer-mobile'
import API from './assets/js/app/api'
import common from './assets/js/app/common'
import VueBus from './assets/js/app/vue-bus'
import 'swiper/dist/css/swiper.css'
import './assets/css/reset.css'
import './assets/css/animate.css'
import './registerServiceWorker'
import Loading from './components/Common/Loading.vue'
import Message from './components/Common/Message'
import PageTitle from './components/Common/PageTitle.vue'
// Vue.use(Toast)
Vue.use(layer)
Vue.use(VueAwesomeSwiper)
Vue.use(VueLazyload)
Vue.use(Message)

Vue.use(Navigation, { router, store })
Vue.component('Loading', Loading)
Vue.component('PageTitle', PageTitle)
Vue.prototype.$API = API
Vue.prototype.$BASEPATH = common.BASEPATH
Vue.config.productionTip = false
Vue.prototype.$axios = axios
Vue.prototype.$md5 = md5
Vue.prototype.$APPKEY = APPKEY
Vue.prototype.$cookies = cookies
Vue.prototype.$bus = VueBus

router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  console.log('lll')
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // this route requires auth, check if logged in
    // if not, redirect to login page.
    if (!store.state.global.isLogIn) {
      next({
        path: '/login',
        query: { redirect: to.fullPath } //把要跳转的地址作为参数传到下一步
      })
    } else {
      next()
    }
  } else {
    next() // 确保一定要调用 next()
  }
})

new Vue({
  router,
  store,
  API,
  $,
  // md5,
  VueBus,
  cookies,
  axios,
  APPKEY,
  render: h => h(App),
  methods: {
    addIconLink(link) {
      let s = document.createElement('script')
      s.type = 'text/javascript'
      s.src = link
      document.body.appendChild(s)
    }
  },
  mounted() {
    this.addIconLink('https://at.alicdn.com/t/font_738716_a7zhybgoyxk.js')
  }
}).$mount('#app')
